package com.greet.controller.admin;

import com.greet.dto.CategoryDTO;
import com.greet.dto.CategoryPageQueryDTO;
import com.greet.entity.Category;
import com.greet.result.PageResult;
import com.greet.result.Result;
import com.greet.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id){
        categoryService.setStatus(status, id);
        return Result.success();
    }

    @PostMapping
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteById(Long id){
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectByType(Integer type){
        List<Category> categoryList = categoryService.selectByType(type);
        return Result.success(categoryList);
    }
}
